import { GoodsComments } from './goodsComment';
let _GoodsComments = GoodsComments;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import GoodsCommentMock from './mallm/goods/goodsCommentMock';
 * GoodsCommentMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/goods/goodsComment/listKey').reply(config => {
      let params = config.params;
      let mockGoodsComments = _GoodsComments.filter(goodsComment => {
      	if(params.id||params.id==''){ if( goodsComment.id.indexOf(params.id)>=0) return true;}
      	if(params.productId||params.productId==''){ if( goodsComment.productId.indexOf(params.productId)>=0) return true;}
      	if(params.remark||params.remark==''){ if( goodsComment.remark.indexOf(params.remark)>=0) return true;}
      	if(params.star||params.star==''){ if( goodsComment.star.indexOf(params.star)>=0) return true;}
      	if(params.time||params.time==''){ if( goodsComment.time.indexOf(params.time)>=0) return true;}
      	if(params.orderid||params.orderid==''){ if( goodsComment.orderid.indexOf(params.orderid)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( goodsComment.deptid.indexOf(params.deptid)>=0) return true;}
      	if(params.userid||params.userid==''){ if( goodsComment.userid.indexOf(params.userid)>=0) return true;}
      	if(params.goodsId||params.goodsId==''){ if( goodsComment.goodsId.indexOf(params.goodsId)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockGoodsComments
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/goods/goodsComment/list').reply(config => {
      let params = config.params;
      let mockGoodsComments = _GoodsComments.filter(goodsComment => {
      	var isTrue=false;
      	if(params.id||params.id==''){if(goodsComment.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.productId||params.productId==''){if(goodsComment.productId.indexOf(params.productId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.remark||params.remark==''){if(goodsComment.remark.indexOf(params.remark)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.star||params.star==''){if(goodsComment.star.indexOf(params.star)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.time||params.time==''){if(goodsComment.time.indexOf(params.time)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.orderid||params.orderid==''){if(goodsComment.orderid.indexOf(params.orderid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(goodsComment.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.userid||params.userid==''){if(goodsComment.userid.indexOf(params.userid)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.goodsId||params.goodsId==''){if(goodsComment.goodsId.indexOf(params.goodsId)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockGoodsComments
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockGoodsComments.length;
      mockGoodsComments = mockGoodsComments.filter((goodsComment, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockGoodsComments,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/goods/goodsComment/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _GoodsComments = _GoodsComments.filter(goodsComment => goodsComment.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/goods/goodsComment/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _GoodsComments = _GoodsComments.filter(goodsComment => !params.includes(goodsComment.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/goods/goodsComment/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { id,productId,remark,star,time,orderid,deptid,userid,goodsId } = params;
      _GoodsComments.some(goodsComment => {
        if (goodsComment.id === id) {
			goodsComment.id = id;
			goodsComment.productId = productId;
			goodsComment.remark = remark;
			goodsComment.star = star;
			goodsComment.time = time;
			goodsComment.orderid = orderid;
			goodsComment.deptid = deptid;
			goodsComment.userid = userid;
			goodsComment.goodsId = goodsId
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/goods/goodsComment/add').reply(config => {
      var params = JSON.parse( config.data );
      let { id,productId,remark,star,time,orderid,deptid,userid,goodsId } = params;
      _GoodsComments.push({
		id:id,
		productId:productId,
		remark:remark,
		star:star,
		time:time,
		orderid:orderid,
		deptid:deptid,
		userid:userid,
		goodsId:goodsId
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};